package com.cloudfun.campusshare.device.protocol.decoder;

import com.cloudfun.campusshare.device.protocol.response.GxyAlarm;
import org.apache.commons.lang.math.NumberUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * 滚筒洗衣机
 * Created by Huqin on 2020/1/13
 */
public class GxyAlarmMessageDecoder extends ByteToMessageDecoder<GxyAlarm> {

    private List<FieldDecoder<GxyAlarm>> fieldDecoderList;

    public GxyAlarmMessageDecoder() {
        fieldDecoderList = new ArrayList<>();
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(11, 1, this.alarm1Map()));
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(10, 1, this.alarm2Map()));
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(9, 1, this.alarm3Map()));
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(8, 1, this.alarm4Map()));
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(7, 1, this.alarm5Map()));
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(5, 1, this.alarm7Map()));
    }

    @Override
    protected List<FieldDecoder<GxyAlarm>> fieldDecoderList() {
        return fieldDecoderList;
    }

    @Override
    protected GxyAlarm prepare() {
        return new GxyAlarm();
    }

    @Override
    protected boolean preProcessBeforeDecode(byte[] data, GxyAlarm obj) {
        for (byte byt : data) {
            if (NumberUtils.BYTE_ZERO != byt) {
                return false;
            }
        }
        obj.setAlarmCancel(true);
        return true;
    }

    public Map<Integer, BiConsumer<GxyAlarm, Boolean>> alarm1Map() {
        Map<Integer, BiConsumer<GxyAlarm, Boolean>> map = new HashMap<>();
        // map.put(3, GxyAlarm::setWaterHeatingErr);
        map.put(4, GxyAlarm::setMotorStopAlarm);
        map.put(5, GxyAlarm::setMotorStopAlarm);
        map.put(6, GxyAlarm::setMotorStopAlarm);
        map.put(7, GxyAlarm::setMotorStopAlarm);
        return map;
    }

    public Map<Integer, BiConsumer<GxyAlarm, Boolean>> alarm2Map() {
        Map<Integer, BiConsumer<GxyAlarm, Boolean>> map = new HashMap<>();
        map.put(0, GxyAlarm::setMotorStopAlarm);
        map.put(1, GxyAlarm::setMotorStopAlarm);
        map.put(2, GxyAlarm::setMotorStopAlarm);
        map.put(3, GxyAlarm::setMotorStopAlarm);
        map.put(4, GxyAlarm::setMotorStopAlarm);
        map.put(5, GxyAlarm::setMotorStopAlarm);
        map.put(6, GxyAlarm::setMotorStopAlarm);
        map.put(7, GxyAlarm::setMotorStopAlarm);
        return map;
    }

    public Map<Integer, BiConsumer<GxyAlarm, Boolean>> alarm3Map() {
        Map<Integer, BiConsumer<GxyAlarm, Boolean>> map = new HashMap<>();
        map.put(3, GxyAlarm::setTempSensorErr);
        map.put(6, GxyAlarm::setWaterHeatingErr);
        map.put(7, GxyAlarm::setWaterHeatingErr);
        return map;
    }

    public Map<Integer, BiConsumer<GxyAlarm, Boolean>> alarm4Map() {
        Map<Integer, BiConsumer<GxyAlarm, Boolean>> map = new HashMap<>();
//        map.put(0, GxyAlarm::setFcBoardCommErr);
//        map.put(1, GxyAlarm::setDriverCommErr);
        map.put(4, GxyAlarm::setWaterLevelSensorErr);
        map.put(5, GxyAlarm::setInWaterErr);
        map.put(6, GxyAlarm::setDrainErr);
//        map.put(7, GxyAlarm::setWaterLevelAlarm);
        return map;
    }

    public Map<Integer, BiConsumer<GxyAlarm, Boolean>> alarm5Map() {
        Map<Integer, BiConsumer<GxyAlarm, Boolean>> map = new HashMap<>();
//        map.put(3, GxyAlarm::setWaterLevelAlarm);
        map.put(4, GxyAlarm::setSpinUnbalanceAlarm);
        return map;
    }

    public Map<Integer, BiConsumer<GxyAlarm, Boolean>> alarm7Map() {
        Map<Integer, BiConsumer<GxyAlarm, Boolean>> map = new HashMap<>();
        map.put(0, GxyAlarm::setDoorCloseErr);
        return map;
    }

}
